home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1994 October / Macformat17.cdr / Shareware City / Developers / shutdown-fx-201-c / sfx ƒ / sfx startup app ƒ / sfx code ƒ / sfx install.c < prev    next >
Text File  |  1994-07-11  |  4KB  |  137 lines

  1. /**********************************************************************\
  2.  
  3. File:        sfx install.c
  4.  
  5. Purpose:    This module handles installing shutdown procs and installing
  6.             and removing fades from the system heap.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "Shutdown.h"
  26. #include "sfx install.h"
  27. #include "sfx gestalt.h"
  28. #include "program globals.h"
  29.  
  30. short InstallTheShutdownProc(Str255 theName, short vRefNum, long dirID,
  31.     Boolean onShutdown, Boolean onRestart, Boolean isEarly)
  32. /* assume: there are no shutdown procs currently installed */
  33. /* assume: sfx Gestalt function is properly installed */
  34. {
  35.     OSErr            theResError;
  36.     FSSpec            fs;
  37.     short            oldRefNum, newRefNum;
  38.     Boolean            alreadyOpen;
  39.     Handle            theProcHandle;
  40.     ProcPtr            theFade, theShutdownProc, theRestartProc;
  41.     unsigned long    theSize;
  42.     unsigned long    dummy;
  43.     short            resultCode;
  44.     
  45.     BlockMove(theName, fs.name, theName[0]+1);
  46.     fs.vRefNum=vRefNum;
  47.     fs.parID=dirID;
  48.     theResError=OpenTheResFile(&fs, &oldRefNum, &newRefNum, &alreadyOpen);
  49.     if (theResError!=noErr)
  50.         return kCantOpenModule;
  51.     
  52.     theProcHandle=Get1Resource('PROC', 0);
  53.     if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  54.         return kModuleDamaged;
  55.         
  56.     if (*theProcHandle==0L)
  57.         LoadResource(theProcHandle);
  58.     if (*theProcHandle==0L)
  59.         return kModuleDamaged;
  60.         
  61.     theProcHandle=StripAddress(theProcHandle);
  62.     *theProcHandle=StripAddress(*theProcHandle);
  63.         
  64.     HLock(theProcHandle);
  65.     theFade=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  66.     if (theFade==0L)
  67.     {
  68.         ReleaseResource(theProcHandle);
  69.         return kNoMemoryInSystemHeap;
  70.     }
  71.     
  72.     theFade=StripAddress(theFade);
  73.         
  74.     BlockMove(*theProcHandle, theFade, theSize);
  75.     ReleaseResource(theProcHandle);
  76.     theProcHandle=0L;
  77.     CloseTheResFile(oldRefNum, newRefNum, alreadyOpen);
  78.     
  79.     theProcHandle=Get1Resource('PROC', 10);    /* get shutdown proc from sfx init */
  80.     if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  81.         return kCantGetProc10Resource;
  82.  
  83.     if (*theProcHandle==0L)
  84.         LoadResource(theProcHandle);
  85.     if (*theProcHandle==0L)
  86.     {
  87.         DisposePtr(theFade);
  88.         return kCantGetProc10Resource;
  89.     }
  90.     
  91.     theProcHandle=StripAddress(theProcHandle);
  92.     *theProcHandle=StripAddress(*theProcHandle);
  93.         
  94.     theShutdownProc=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  95.     theRestartProc=NewPtrSys(theSize);
  96.     
  97.     theShutdownProc=StripAddress(theShutdownProc);
  98.     theRestartProc=StripAddress(theRestartProc);
  99.     
  100.     BlockMove(*theProcHandle, theShutdownProc, theSize);
  101.     BlockMove(*theProcHandle, theRestartProc, theSize);
  102.     ReleaseResource(theProcHandle);
  103.     theProcHandle=0L;
  104.     
  105.     *(ProcPtr*)((long)theShutdownProc+6)=
  106.         *(ProcPtr*)((long)theRestartProc+6)=theFade;            /* store address of fade */
  107.     *(short*)((long)theShutdownProc+10)=onShutdown ? 1 : 0;        /* store on shutdown flag */
  108.     *(short*)((long)theRestartProc+10)=onRestart ? 1 : 0;        /* store on restart flag */
  109.     ShutDwnInstall(theShutdownProc, isEarly ? sdOnPowerOff+sdOnDrivers : sdOnPowerOff);
  110.     ShutDwnInstall(theRestartProc, isEarly ? sdOnRestart+sdOnDrivers : sdOnRestart);
  111.     resultCode=SetGestaltProcPtrs(theFade, theShutdownProc, theRestartProc);
  112.     
  113.     return resultCode;
  114. }
  115.  
  116. OSErr OpenTheResFile(FSSpec* fs, short* oldRefNum, short* newRefNum, short* alreadyOpen)
  117. {
  118.     unsigned long    oldTopMapHndl;
  119.     OSErr            theResError;
  120.     
  121.     *oldRefNum=CurResFile();
  122.     oldTopMapHndl=(unsigned long)TopMapHndl;
  123.     *newRefNum=HOpenResFile(fs->vRefNum, fs->parID, fs->name, fsRdPerm);
  124.     theResError=ResError();
  125.     *alreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
  126.     return theResError;
  127. }
  128.  
  129. void CloseTheResFile(short oldRefNum, short newRefNum, Boolean alreadyOpen)
  130. {
  131.     if (!alreadyOpen)
  132.     {
  133.         CloseResFile(newRefNum);
  134.     }
  135.     UseResFile(oldRefNum);
  136. }
  137.